package 原型模式.深拷贝.字节流对象流;

import java.io.*;

public class Sheep implements Cloneable {
    String name;
    Sheep friends;  // 深拷贝：也为引用类型分配存储空间

    public Sheep(String name) {
        this.name = name;
    }

    @Override
    protected Object clone() {
        try {
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(this);
            ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
            ObjectInputStream oi = new ObjectInputStream(bi);
            Object clone = oi.readObject();
            return clone;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        Sheep sheep1 = new Sheep("第一只羊");
        Sheep sheep2 = new Sheep("第二只羊");
        sheep1.friends = sheep2;

        System.out.println(sheep1.friends == sheep2);// true

    }


}
